using AutoMapper;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.Application.Services
{
    /// <summary>
    /// 预约服务实现
    /// </summary>
    public class AppointmentService : IAppointmentService
    {
        private readonly IAppointmentRepository _appointmentRepository;
        private readonly IStudentRepository _studentRepository;
        private readonly ITeacherRepository _teacherRepository;
        private readonly IMapper _mapper;

        public AppointmentService(IAppointmentRepository appointmentRepository, IStudentRepository studentRepository, ITeacherRepository teacherRepository, IMapper mapper)
        {
            _appointmentRepository = appointmentRepository;
            _studentRepository = studentRepository;
            _teacherRepository = teacherRepository;

            _mapper = mapper;
        }

        public async Task<PagedResult<AppointmentDto>> GetPagedListAsync(AppointmentPageRequest pageRequest)
        {
            var result = await _appointmentRepository.GetPagedListAsync(pageRequest);
            return new PagedResult<AppointmentDto>
            {
                Total = result.Total,
                Items = _mapper.Map<List<AppointmentDto>>(result.Items)
            };
        }

        public async Task<AppointmentDto> GetByIdAsync(Guid id)
        {
            var appointment = await _appointmentRepository.GetDetailsByIdAsync(id);
            if (appointment == null)
            {
                throw new ArgumentException($"预约不存在: {id}");
            }

            return _mapper.Map<AppointmentDto>(appointment);
        }

        public async Task<AppointmentDto> CreateAsync(CreateAppointmentDto input)
        {
            var student = await _studentRepository.GetByIdAsync(input.StudentId);
            if (student == null)
            {
                throw new ArgumentException($"学生不存在: {input.StudentId}");
            }
            if (input.TeacherId.HasValue)
            {
                var teacher = await _teacherRepository.GetByIdAsync(input.TeacherId?? Guid.Empty);
                if (teacher == null)
                {
                    throw new ArgumentException($"老师不存在: {input.TeacherId}");
                }
            }
            

            var appointment = Appointment.Create(
                input.OrderId,
                input.StudentId,
                input.TeacherId ?? Guid.Empty,
                input.Deadline,
                input.Amount ?? 0,
                input.Remarks ?? string.Empty 
            );

            await _appointmentRepository.AddAsync(appointment);
            return await GetByIdAsync(appointment.Id);
        }

        public async Task<AppointmentDto> UpdateAsync(UpdateAppointmentDto input)
        {
            var appointment = await _appointmentRepository.GetByIdAsync(input.Id);
            if (appointment == null)
            {
                throw new ArgumentException($"预约不存在: {input.Id}");
            }
            if (input.TeacherId.HasValue)
            {
                var teacher = await _teacherRepository.GetByIdAsync(input.TeacherId ?? Guid.Empty);
                if (teacher == null)
                {
                    throw new ArgumentException($"老师不存在: {input.TeacherId}");
                }
            }
            appointment.TeacherId = input.TeacherId ?? appointment.TeacherId;
            appointment.Status = input.Status?? appointment.Status;
            appointment.Amount = input.Amount ?? appointment.Amount;
            appointment.Remarks = input.Remarks ?? appointment.Remarks;


            await _appointmentRepository.UpdateAsync(appointment);
            return await GetByIdAsync(appointment.Id);
        }

        /// <summary>
        /// 根据预约ID批量更新预约学生
        /// </summary>
        public async Task<List<AppointmentDto>> UpdateRangeAsync(List<Guid> appointmentIds)
        {
            var appointments = await _appointmentRepository.GetByIdsAsync(appointmentIds);
            if (appointments.Count != appointmentIds.Count)
            {
                throw new ArgumentException($"预约不存在: {appointmentIds.Select(x => x.ToString()).ToList()}");
            }
            //批量更新预约状态
            foreach (var appointment in appointments)
            {
                appointment.Status = AppointmentStatus.Completed;
            }
            await _appointmentRepository.UpdateRangeAsync(appointments);
            return _mapper.Map<List<AppointmentDto>>(appointments);
        }

        public async Task DeleteAsync(Guid id)
        {
            var appointment = await _appointmentRepository.GetByIdAsync(id);
            if (appointment == null)
            {
                throw new ArgumentException($"预约不存在: {id}");
            }

            await _appointmentRepository.DeleteAsync(id);
        }

        public async Task<List<AppointmentDto>> GetByStudentIdAsync(Guid studentId)
        {
            var appointments = await _appointmentRepository.GetByStudentIdAsync(studentId);
            return _mapper.Map<List<AppointmentDto>>(appointments);
        }

        public async Task<List<AppointmentDto>> GetByTeacherIdAsync(Guid teacherId)
        {
            var appointments = await _appointmentRepository.GetByTeacherIdAsync(teacherId);
            return _mapper.Map<List<AppointmentDto>>(appointments);
        }
    }
} 